# 示例1-1: 使用scikit-learn包训练并允许一个线性模型
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.linear_model

# Load the data

oecd_bli = pd.read_csv("oecd_bli_2015.csv", thousands = ',')
gdp_per_capita = pd.read_csv("gdp_per_capita.csv", thousands = ',', delimiter = '\t',
                              encoding = 'latin1', na_values = 'n/a')

# prepare the data

country_stats = prepare_country_stats(oecd_bli, gdp_per_capita)
X = np.c_[country_stats["GDP per capita"]]
Y = np.c_[country_stats["Life satisfaction"]]

# visualize the data

country_stats.plot(kind = 'scatter', x = "GDP per capita", y = "Life satisfaction")
plt.show()

# select a linear model
model = sklearn.linear_model.LinearRegression()

# train the model
model.fit(X, Y)

# make a prediction for cyprus
X_new = [[22587]] # Cyprus's GDP per capita
print(model.predict(X_new)) # outputs[[5.96242338]]


"""
换linear regression为KNN：

import sklearn.linear_model
model = sklearn.linear_model.LinearRegression()

import sklearn.neighbors
model = sklearn.neighbors.kNeighborsRegressor(n_neighbors = 3)

"""
